import { createApp } from "vue";
import App from "./App.vue";
import {
  renderWithQiankun,
  qiankunWindow,
} from "vite-plugin-qiankun/dist/helper";
import { router } from "./router";

window.__POWERED_BY_QIANKUN__ = qiankunWindow.__POWERED_BY_QIANKUN__;
let app: any;

function render() {
  app = createApp(App);
  app.use(router);
  app.mount("#app");
}

// 判断是否独立运行
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  console.log("作为独立应用运行中");
  render();
}

renderWithQiankun({
  bootstrap() {
    console.log("bootstrap");
  },
  mount(props) {
    console.log("Vue 子应用挂载", props);
    render();
  },
  unmount() {
    if (app && typeof app.unmount === "function") {
      app.unmount();
      console.log("vue子应用卸载");
    }
  },
  update() {
    console.log("更新");
  },
});
